<?php

class Generator_Action_Default extends Kernel_Action_Form 
{
    
    /**
     * Path to the template file.
     *
     * @var string
     */
    protected $_template = 'generator/form.tpl';
    
    /**
     * Флаг, указывающий, что после сохранения данных формы не нужно показывать
     * сответствующее сообщение.
     *
     * @var boolean
     */
    protected $_no_message = true;
    
    /**
    * Session key
    * 
    * @var string
    */
    protected $_session_key;
    
    /**
     * Constructor.
     *
     * @var  array   $params
     */
    public function __construct($params = array()) 
    {
        // validate session and on false start new one
        $this->_session_key = Application::getRequest()->get('session');
        if (!Generator::isSessionValid($this->_session_key)) 
        {
            $this->_location(array('session' => Generator::startSession()));
        }
        
        parent::__construct($params);
    }
    
    /**
    * Initialize form elements.
    * 
    */
    protected function _initElements() 
    {
        $cycle = range(1, $this->_getRowsNumber());
        
        $this->_addElement('tracklist', array(
            'type' => 'file', 
            'save' => array($this, 'file'), 
            'validators' => array
            (
                array('file', array('type_allowed' 
                    => Kernel_Config::get('generator', 'type_allowed'))), 
                array('Generator_Validator_Tracklist', array('separator' 
                    => Kernel_Config::get('generator', 'default_perfomer_title_separator'))), 
            )
        ));
        
        /*$this->_addElement('separator', array(
            'caption' => __('SEPARATOR'), 
            'value' => Kernel_Config::get('generator', 'default_perfomer_title_separator'), 
            'no-trim' => true, 
            'notice' => '<strong>Separator</strong> is the perfomer/title delimiter in tracklist', 
        ));
        $file_types = Kernel_Config::get('generator', 'file_types');
        $this->_addElement('file_type', array(
            'type' => 'list', 
            'caption' => __('FILE_TYPE'), 
            'values' => array_combine($file_types, $file_types), 
            'notice' => 'Media file type', 
        ));*/
        
        $this->_addElement('perfomer_common', array('caption' => __('perfomer')));
        $this->_addElement('title_common', array('caption' => __('title')));
        $this->_addElement('file_common', array('caption' => __('file')));
        
        foreach ($cycle as $i) {
            $this->_addElement('perfomer[' . $i . ']');
            $this->_addElement('title[' . $i . ']');
            
            $this->_addElement('index[' . $i . '][hr]', array('class' => 'hr'));
            $this->_addElement('index[' . $i . '][mn]', array('class' => 'mn'));
            $this->_addElement('index[' . $i . '][sc]', array('class' => 'sc'));
            $this->_addElement('index[' . $i . '][fr]', array('class' => 'fr'));
        }
        
        Application::getResponse()->assign('rows_number', count($cycle));
        
        $this->_setValues($this->_model()->getValues($this->_session_key));
    }
    
    /**
     * Возвращает набор данный формы, которые нужно передать в шаблон.
     *
     * @return  array
     */
    public function getTemplateData() 
    {
        $data = parent::getTemplateData();
        
        $elements =& $data['elements'];
        
        
        if (!$elements['index[1][hr]']['value']) {
            $elements['index[1][hr]']['value'] = '00';
        }
        if (!$elements['index[1][mn]']['value']) {
            $elements['index[1][mn]']['value'] = '00';
        }
        if (!$elements['index[1][sc]']['value']) {
            $elements['index[1][sc]']['value'] = '00';
        }
        if (!$elements['index[1][fr]']['value']) {
            $elements['index[1][fr]']['value'] = '00';
        }
        
        return $data;
    }
    
    /**
    * Return model object
    * 
    * @return Generator_Model
    */
    protected function _model() 
    {
        static $cach;
        
        if (is_null($cach)) {
            $cach = new Generator_Model;
        }
        
        return $cach;
    }
    
    /**
    * Return number of rows
    * 
    * @return integer
    */
    protected function _getRowsNumber() 
    {
        return $this->_model()->countRows($this->_session_key);
    }
    
}